<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html> <head>
<title>Sets Tests</title>
<script src=../../src/util/sets.js></script>
</head>

<body>
<h1>Sets Tests</h1>

<style>
.running { background-color: yellow }
.pass { background-color: #afa }
.fail { background-color: #faa }
</style>

<ul id=testcases>
  <li>[] &#x222A [] = []</li>
  <li>[] - [] = []</li>
  <li>[1] &#x222A [2] = [1, 2]</li>
  <li>[1] - [2] = [1]</li>
  <li>[2] - [1] = [2]</li>
  <li>[1] - [1] = []</li>
  <li>[1, 2, 3] &#x222A; [2] = [1, 2, 3]</li>
  <li>[1, 2, 3] &#x222A; [4] = [1, 2, 3, 4]</li>
  <li>[1, 2, 3] &#x222A; [0] = [0, 1, 2, 3]</li>
  <li>[1, 2, 3] &#x222A; [1, 3, 4] = [1, 2, 3, 4]</li>
  <li>[1, 5, 7, 8, 9] &#x222A; [2, 3, 4, 5, 6, 7] = [1, 2, 3, 4, 5, 6, 7, 8, 9]</li>
  <li>[1, 2, 3] - [2] = [1, 3]</li>
  <li>[1, 2, 3] - [4] = [1, 2, 3]</li>
  <li>[4] - [1, 2, 3] = [4]</li>
  <li>[1, 2, 3] - [0] = [1, 2, 3]</li>
  <li>[0] - [1, 2, 3] = [0]</li>
  <li>[1, 2, 3] - [1, 3, 4] = [2]</li>
  <li>[1, 3, 4] - [1, 2, 3] = [4]</li>
  <li>[1, 5, 7, 8, 9] - [2, 3, 4, 5, 6, 7] = [1, 8, 9]</li>
  <li>[2, 3, 4, 5, 6, 7] - [1, 5, 7, 8, 9] = [2, 3, 4, 6]</li>
</ul>

<script>
(function () {
  var operators = { '\u222a' : { op: set_union, associative: true },
                    '-' : { op: set_difference, associative: false } };
  var testcaseCont = document.getElementById('testcases');
  for (var testcase = testcaseCont.firstChild; testcase;
       testcase = testcase.nextSibling) (function (testcase) {
    if (testcase.nodeType !== 1) { return; }
    setTimeout(function () {
      var testText = testcase.firstChild.nodeValue;
      var hasConsole = typeof console !== 'undefined' && console.group;
      if (hasConsole) { console.group(testText); }
      try {
        testcase.className = 'running';
        var m = testText.match(
            /^\s*(\[[^\]]*\])\s*([^\s\[]+)\s*(\[[^\]]*\])\s*=\s*(\[[^\]]*\])\s*$/);
        var a = eval(m[1]), b = eval(m[3]), op = operators[m[2]],
            expected = eval(m[4]);
        var result = op.op(a, b);
        if (op.associative && ('' + result) === ('' + expected)) {
          result = op.op(b, a);
        }
        var passed = ('' + result) === ('' + expected);
        var message;
        if (!passed) {
          message = 'was ' + result;
        } else if (('' + result) === ('' + a) || ('' + result) === ('' + b)) {
          if (result !== a && result !== b) {
            passed = false;
            message = 'unnecessary object creation';
          }
        }
        if (!passed) {
          testcase.appendChild(document.createTextNode('; was ' + result));
        }
        testcase.className = passed ? 'pass' : 'fail';
      } finally {
        if (hasConsole) { console.groupEnd(); }
      }
    }, 0);
  })(testcase);
})();
</script>

<hr>
<address></address>
<!-- hhmts start --> Last modified: Wed Dec 30 19:11:56 PST 2009 <!-- hhmts end -->
</body> </html>
